package com.sport.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sport.bean.Admin;
import com.sport.bean.PageParam;
import com.sport.bean.PageResult;
import com.sport.bean.Teacher;
import com.sport.service.TeacherService;
import com.sport.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Controller
public class TeacherController {
    @Autowired
    private TeacherService teacherService;


    /*
    分页查询管理员
     */
    @GetMapping(value = "/teacher/list")
    @ResponseBody
    public PageResult<Teacher> findAll1(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                        @RequestParam(value = "limit", defaultValue = "10") Integer limit,
                                        HttpServletRequest request) {
        PageHelper.startPage(page, limit);
        PageParam<Teacher> pageParam = new PageParam<>(request);
        List<Teacher> teachers = teacherService.listPage(pageParam);
        PageInfo info = new PageInfo(teachers);
        return new PageResult<Teacher>(teachers, info.getTotal());
    }

    /*
    删除
     */
    @GetMapping("/teacher/del")
    @ResponseBody
    public JsonResult delAdmin(Integer id) {
        if (teacherService.deletTeacher(id) > 0) {
            return JsonResult.ok("删除成功");
        } else {
            return JsonResult.error("删除失败");
        }
    }

    //id查找
    @GetMapping("/teacher/findById")
    @ResponseBody
    public  Teacher findId(Integer id){
        return  teacherService.findById(id);
    }
    //修改裁判
    @GetMapping("/teacher/updata")
    @ResponseBody
    public  JsonResult updataTeacher(MultipartFile file,Teacher teacher){
        if (teacherService.updateTea(teacher) >0){
            return  JsonResult.ok("修改成功");
        }else {
            return  JsonResult.error("修改失败");
        }
    }
    /**
     添加裁判
     */
    @GetMapping("/teacher/add")
    @ResponseBody
    public JsonResult addTeacher(MultipartFile file, Teacher teacher) {


        if ( teacherService.addTeacher(teacher) > 0) {
            return JsonResult.ok("添加成功");
        } else {
            return JsonResult.error("添加失败");
        }

    }

    /**
     根据裁判名字查询
     */
    @GetMapping("/teacher/findByname")
    @ResponseBody
    public JsonResult findByname(String name) {
        if (teacherService.findByName(name)!=null){
            return JsonResult.error("用户名已存在");
        }else {
            return null;
        }
    }

}